<?php
namespace _Persoonbeheer\Model;
class Info{
    private $_idPersoon = 0;
    private $_dbVerbinding = null;
    private $_json = ['resultaat'=>true,'info'=>[],'adres'=>null,'groep'=>null,'medewerker'=>null,'webroot'=>''];
    public function index(){
        $this->_json['webroot']=  \Instelling::GET('website.root');
        $this->_idPersoon = filter_input(INPUT_POST,'id',FILTER_SANITIZE_NUMBER_INT);
        $this->_dbVerbinding =\Db::GET();
        $this->_info();
        $this->_adres();
        $this->_groep();
        $this->_medewerker();
        //print_r($this->_json);
        return $this->_json;
    }
    private function _info(){
        $dbSql = "SELECT * FROM persoon WHERE id=:idpersoon";
        try {
            $dbStmt = $this->_dbVerbinding->prepare($dbSql);
            $dbStmt->execute(['idpersoon'=>  $this->_idPersoon]);
            if($dbStmt->rowCount()==0){
                $this->_json['resultaat']= FALSE;
                      
            }else{
                $dbRij = $dbStmt->fetch(\PDO::FETCH_ASSOC);
                $info=[];
                $info['aanhef'] = $dbRij['aanhef']=='M'?'Dhr.':'Mevr.';
                $info['naam']=  implode(' ',[$dbRij['naam'],$dbRij['voornaam'],$dbRij['initialen'] ]);
                $tmp = \DateTime::createFromFormat('Y-m-d',$dbRij['geborenop']);
                $info['geborenop']= $tmp->format('d-m-Y');
                $info['email']=$dbRij['email'];
                $info['mobiel']=$dbRij['mobiel'];
                $info['foto']=$dbRij['foto'];
                switch ($dbRij['status']) {
                    case 1:
                        $info['status']='niet actief'; 
                        break;
                    case 2:
                        $info['status']='geblokeerd'; 
                        break;
                    default:
                        $info['status']='actief'; 
                        break;
                }
                $tmp = \DateTime::createFromFormat('Y-m-d',$dbRij['datumnieuw']);
                $info['datumnieuw']= $tmp->format('d-m-Y');
                $tmp = \DateTime::createFromFormat('Y-m-d',$dbRij['datumbezoek']);
                $info['datumbezoek']= $tmp->format('d-m-Y');
                $this->_json['info']=$info;
            }
        } catch (\PDOException $ex) {
            $this->_json['resultaat']=false;
        }
    }
    private function _adres(){
       $adres = [];
       $dbSql = "SELECT * FROM persoonadres WHERE idpersoon=:idpersoon";
       try {
            $dbStmt = $this->_dbVerbinding->prepare($dbSql);
            $dbStmt->execute([':idpersoon'=>$this->_idPersoon]);
            if($dbStmt->rowCount()==0){
                //echo 'helaas'.$dbSql.$this->_idPersoon;
                return ;//[];
            }else{
                //echo 'suc';
                $i =0;
                while ($dbAdres = $dbStmt->fetch(\PDO::FETCH_ASSOC)){
                    $soort = $dbAdres['soort'];
                    $tmp = [];
                    $tmp['adres']=  implode(' ',[$dbAdres['straat'],$dbAdres['nummer'],'bus',$dbAdres['bus']]);
                    $tmp['plaats']=  implode(' ',[$dbAdres['postcode'],$dbAdres['gemeente']]);
                    $tmp['provincie']= empty($dbAdres['state'])?'-' :$dbAdres['state'];
                    $tmp['land']=  $dbAdres['land'];
                    $adres[$soort]=  $tmp;
                }
                $this->_json['adres']=$adres;
            }
        } catch (PDOException $ex){
            $this->_json['resultaat']=false;
        }
    }
    private function _groep(){
        $dbSQL ="( SELECT DISTINCT g.groep as groep , 1 as id
                        FROM groep g JOIN persoongroep p 
                        on g.id = p.idgroep
                        WHERE p.idpersoon = :idpersoon )
                        UNION
                        (SELECT DISTINCT groep as groep , 0 as id 
                        FROM groep
                        WHERE id NOT IN (SELECT idgroep 
                            FROM persoongroep WHERE idpersoon=:idpersoon))
                        ORDER BY id DESC, groep";
        try {
           $dbStmt = $this->_dbVerbinding->prepare($dbSQL);
           $dbStmt->execute([':idpersoon'=>$this->_idPersoon,
                            ':idpersoon'=>$this->_idPersoon]);
           if($dbStmt->rowCount()>0){
               $groep =[];
               while ($dbRij = $dbStmt->fetch(\PDO::FETCH_ASSOC)){
                   array_push($groep, [$dbRij['groep'],$dbRij['id']]);
               }
               $this->_json['groep']=$groep;
           }
        } catch (PDOException $ex) {
        }
    }
    private function _medewerker(){
        $dbSQL ="( SELECT DISTINCT b.groep as groep , 1 as id
                        FROM beheer b JOIN persoonbeheer p 
                        on b.id = p.idbeheer
                        WHERE p.idpersoon = :idpersoon )
                        UNION
                        (SELECT DISTINCT groep as groep , 0 as id 
                        FROM beheer
                        WHERE id NOT IN (SELECT idbeheer 
                            FROM persoonbeheer WHERE idpersoon=:idpersoon))
                        ORDER BY id DESC, groep";
        try {
           $dbStmt = $this->_dbVerbinding->prepare($dbSQL);
           $dbStmt->execute([':idpersoon'=>$this->_idPersoon,
                            ':idpersoon'=>$this->_idPersoon]);
           if($dbStmt->rowCount()>0){
               $medewerker =[];
               while ($dbRij = $dbStmt->fetch(\PDO::FETCH_ASSOC)){
                   array_push($medewerker, [$dbRij['groep'],$dbRij['id']]);
               }
               $this->_json['medewerker']=$medewerker;
           }
        } catch (PDOException $ex) {
        }
    }
}



